package morenoapp.resources;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import morenoapp.tools.MorenoTool;
import morenoapp.util.MorenoUtil;

/**
 *
 * @author dmitry.mamonov
 */
public class ResourcesHolder {
    private static final Logger log = Logger.getLogger(ResourcesHolder.class.getName());

    public static final Icon iconHelpSmall = new ImageIcon(ResourcesHolder.class.getResource("help-16.png"));
    public static final Icon iconNavigatePrevSmall = new ImageIcon(ResourcesHolder.class.getResource("navigate_prev-16.png"));
    public static final Icon iconNavigateNextSmall = new ImageIcon(ResourcesHolder.class.getResource("navigate_next-16.png"));
    public static final Icon iconMoveUpSmall = new ImageIcon(ResourcesHolder.class.getResource("up-16.png"));
    public static final Icon iconMoveDownSmall = new ImageIcon(ResourcesHolder.class.getResource("down-16.png"));
    public static final Icon iconDeleteSmall = new ImageIcon(ResourcesHolder.class.getResource("delete-16.gif"));
    public static final Icon iconAddSmall = new ImageIcon(ResourcesHolder.class.getResource("add-16.png"));
    public static final Icon iconPrintSmall = new ImageIcon(ResourcesHolder.class.getResource("print-16.png"));

    //generated:begin
    public static Icon ICON_IMAGE_ABOUT;
    static {
        try {
            ICON_IMAGE_ABOUT = new ImageIcon(ResourcesHolder.class.getResource("image_about.png"));
        } catch(Exception ex) {
            log.log(Level.SEVERE, null,ex);
        }
    }
    //generated:end

    protected static List<String> updateResources(Class<?> clazz){
        List<String> generatedCode = new ArrayList<String>();
        File path = new File(clazz.getResource(".").toString().substring("file:/".length()));
        System.out.println("path = " + path);
        for (File f : path.listFiles()) {
            System.out.println("file: "+f.getName());
            for (String key : new String[]{"action_", "message_", "image_"}) {
                if (f.getName().startsWith(key)) {
                    System.out.println("updating "+f.getName()+"...");
                    String fieldName = "ICON_"+f.getName().toUpperCase().replace("-", "_").replace(".PNG", "").replace("16", "SMALL").replace("32", "MED");
                    String resName = f.getName();
                    generatedCode.add(String.format("    public static ImageIcon %s;", fieldName));
                    generatedCode.add(String.format("    static {"));
                    generatedCode.add(String.format("        try {"));
                    generatedCode.add(String.format("            %s = new ImageIcon(%s.class.getResource(\"%s\"));", fieldName, clazz.getSimpleName(), resName));
                    generatedCode.add(String.format("        } catch(Exception ex) {"));
                    generatedCode.add(String.format("            log.log(Level.SEVERE, null,ex);"));
                    generatedCode.add(String.format("        }"));
                    generatedCode.add(String.format("    }"));
                }
            }
        }
        return generatedCode;
    }

    public static void main(String[] args) throws IOException {
        MorenoTool.updateSourceFile(ResourcesHolder.class, updateResources(ResourcesHolder.class));
    }

}
